package com.codedancer.campus.client.api.commodity;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.commodity.ro.CommodityAddOrUpdateRo;
import com.codedancer.campus.client.api.commodity.ro.CommodityPageSearchRo;
import com.codedancer.campus.client.service.ICommodityService;
import com.codedancer.campus.client.service.bo.commodity.CommodityAddOrUpdateBo;
import com.codedancer.campus.client.service.bo.commodity.CommodityPageSearchBo;
import com.codedancer.campus.client.service.vo.commodity.CommodityVo;
import com.codedancer.campus.common.anno.Limit;
import com.codedancer.campus.common.anno.PassToken;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/commodity")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "商品Api", description = "商品Api", tags = "商品Api")
public class CommodityApi extends BaseApi {

    @Autowired
    private ICommodityService commodityService;

    @PostMapping("/publish")
    @ApiOperation("发布商品")
    public ApiResponse<Void> publish(
        @RequestBody @Valid @ApiParam(value = "发布商品Ro", required = true)
        CommodityAddOrUpdateRo commodityAddOrUpdateRo) {
        CommodityAddOrUpdateBo commodityAddOrUpdateBo = BeanUtil.copyProperties(
            commodityAddOrUpdateRo, CommodityAddOrUpdateBo.class);
        commodityAddOrUpdateBo.setCurrentUserUuid(currentLoginUuid());
        commodityService.publish(commodityAddOrUpdateBo);
        return success();
    }

    @GetMapping("/page-search/user")
    @ApiOperation("分页获取用户发布商品")
    public ApiResponse<IPage<CommodityVo>> pageSearchUserCommodity(
        @ApiParam(value = "商品搜索Ro", required = false)
        CommodityPageSearchRo commodityPageSearchRo) {
        CommodityPageSearchBo commodityPageSearchBo = BeanUtil.copyProperties(commodityPageSearchRo,
            CommodityPageSearchBo.class);
        commodityPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(commodityService.pageSearchUserCommodity(commodityPageSearchBo));
    }

    @GetMapping("/page-search")
    @ApiOperation("分页查询商品")
    public ApiResponse<IPage<CommodityVo>> pageSearch(
        @ApiParam(value = "商品搜索Ro", required = false)
        CommodityPageSearchRo commodityPageSearchRo) {
        CommodityPageSearchBo commodityPageSearchBo = BeanUtil.copyProperties(commodityPageSearchRo,
            CommodityPageSearchBo.class);
        commodityPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(commodityService.pageSearch(commodityPageSearchBo));
    }

    @GetMapping("/detail/{commodityUuid}")
    @ApiOperation("商品详情")
    @PassToken
    public ApiResponse<CommodityVo> detail(
        @PathVariable(value = "commodityUuid", required = true) @ApiParam(value = "商品uuid", required = true) String commodityUuid) {
        return success(commodityService.detail(commodityUuid));
    }

    @DeleteMapping("/delete/{commodityUuid}")
    @ApiOperation("删除商品")
    public ApiResponse<Void> delete(
        @PathVariable(value = "commodityUuid", required = true) @ApiParam(value = "商品uuid", required = true) String commodityUuid) {
        commodityService.delete(commodityUuid);
        return success();
    }

    @PutMapping("/drop/{uuid}")
    @ApiOperation("下架")
    public ApiResponse<Void> drop(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.drop(uuid);
        return success();
    }

    @PutMapping("/put/{uuid}")
    @ApiOperation("上架")
    public ApiResponse<Void> put(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.put(uuid);
        return success();
    }

    @PostMapping("/collect/{uuid}")
    @ApiOperation("收藏商品")
    @Limit(key = "COMMODITY_COLLECT", permitsPerSecond = 2, timeout = 500, msg = "系统繁忙，请稍后再试！")
    public ApiResponse<Void> collect(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.collect(uuid, currentLoginUuid());
        return success();
    }

    @GetMapping("/is-collect/{uuid}")
    @ApiOperation("是否收藏")
    public ApiResponse<Boolean> isCollect(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        return success(commodityService.isCollect(uuid, currentLoginUuid()));
    }

    @PutMapping("/cancel-collect/{uuid}")
    @ApiOperation("取消收藏")
    @Limit(key = "COMMODITY_CANCEL_COLLECT", permitsPerSecond = 2, timeout = 500, msg = "系统繁忙，请稍后再试！")
    public ApiResponse<Void> cancelCollect(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.cancelCollect(uuid, currentLoginUuid());
        return success();
    }

    @PutMapping("/edit/{uuid}")
    @ApiOperation("修改商品")
    public ApiResponse<Void> edit(
        @RequestBody @Valid @ApiParam(value = "修改商品Ro", required = true) CommodityAddOrUpdateRo commodityAddOrUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        CommodityAddOrUpdateBo commodityAddOrUpdateBo = BeanUtil
            .copyProperties(commodityAddOrUpdateRo, CommodityAddOrUpdateBo.class);
        commodityAddOrUpdateBo.setUuid(uuid);
        commodityService.edit(commodityAddOrUpdateBo);
        return success();
    }

    @PutMapping("/sync-liberty-square/{uuid}")
    @ApiOperation("同步自由广场")
    public ApiResponse<Void> syncLibertySquare(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.syncLibertySquare(uuid);
        return success();
    }

    @PutMapping("/cancel-sync-liberty-square/{uuid}")
    @ApiOperation("取消同步自由广场")
    public ApiResponse<Void> cancelSyncLibertySquare(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "商品uuid", required = true) String uuid) {
        commodityService.cancelSyncLibertySquare(uuid);
        return success();
    }
}
